'use strict';

const Controller = require('egg').Controller;

class FileController extends Controller {
    // 存储大文件的分片
    async saveChunk(){
        const { ctx } = this;
        const stream = await ctx.getFileStream();
        console.log('stream...', stream);
        try{
            // 分片存储的文件夹
            const chunkDir = path.join(saveDir, `/${stream.fieldname}`)
            if (!fs.existsSync(chunkDir)) {
                fs.mkdirSync(chunkDir);
            }
            // 文件存储路径
            const chunkFile = fs.createWriteStream(path.join(chunkDir, `/${stream.fieldname}_${stream.fields.index}`));
            stream.pipe(chunkFile);
            ctx.body = {'msg': '分片存储成功'}
        }catch(e){
            console.log('e...', e);
            ctx.body = {'msg': '分片存储失败'}
        }
    }
}

module.exports = FileController;


